/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ProjectMgtSystem.Action;

import ProjectMgtSystem.Bean.TaskActionBean;
import ProjectMgtSystem.Entities.Task;
import java.sql.SQLException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.actions.LookupDispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import java.util.*;

/**
 *
 * @author DangDucThinh  
 * StartDate    2011-07-21
 * UpdateDate   2011-07-21
 */
public class TaskAction extends LookupDispatchAction {

    /* forward name="success" path="" */
    private final static String VIEW = "view";
    private final static String FAILURE = "failure";
    private final static String FIRST_UPDATE_ACTION = "viewSelectedID";

// <editor-fold defaultstate="collapsed" desc="getKeyMethodMap">            
    /** Provides the mapping from resource key to method name.
     * @return Resource key / method name map.
     */
    @Override
    protected Map getKeyMethodMap() {
        Map map = new HashMap();
        map.put("TaskAction.view", "view");
        map.put("TaskAction.insert", "insert");
        map.put("TaskAction.delete", "delete");
        map.put("TaskAction.update", "update");
        map.put("TaskAction.select", "select");

        return map;
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="view">    
    /**
     * Action called view action
     */
    public ActionForward view(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws java.lang.Exception {
        TaskActionBean taskForm = (TaskActionBean) form;
        ArrayList<Task> taskList = new java.util.ArrayList<Task>();
        taskList = taskForm.view();
        request.setAttribute("taskList", taskList);
        //+++<Hien 07/25/2011: fix code>
        request.setAttribute("errorMessage", "");
        //---<Hien 07/25/2011: fix code>
        return mapping.findForward(VIEW);   // Forward to admin/TaskInfo.jsp
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="insert">        
    /** Action called on Add button click
     */
    public ActionForward insert(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws java.lang.Exception {
        TaskActionBean taskForm = (TaskActionBean) form;
        String taskID = taskForm.getTaskID();
        String taskName = taskForm.getTaskName();
        String description = taskForm.getDescription();
        //+++<Hien 07/25/2011: fix code>
//        int rowIns = taskForm.insert(taskID, taskName, description);
//        if (rowIns != 0) {
        String message = taskForm.insert(taskID, taskName, description);
        //---<Hien 07/25/2011: fix code>
        ArrayList<Task> taskList = new java.util.ArrayList<Task>();
        taskList = taskForm.view();
        request.setAttribute("taskList", taskList);
        //+++<Hien 07/25/2011: fix code>
        request.setAttribute("errorMessage", message);
        //---<Hien 07/25/2011: fix code>
        return mapping.findForward(VIEW);
        //+++<Hien 07/25/2011: fix code>
//        } else {
//            return mapping.findForward(FAILURE);
//        }
        //---<Hien 07/25/2011: fix code>
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="delete">           
    /** Action called on Delete button click
     */
    public ActionForward delete(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws java.lang.Exception {
        TaskActionBean taskForm = (TaskActionBean) form;
        String taskID = taskForm.getTaskID();
        //+++<Hien 07/25/2011: fix code>
//        int rowDel = taskForm.delete(taskID);
//        if (rowDel != 0) {
        String message = taskForm.delete(taskID);
        //---<Hien 07/25/2011: fix code>
        ArrayList<Task> taskList = new java.util.ArrayList<Task>();
        taskList = taskForm.view();
        request.setAttribute("taskList", taskList);
        //+++<Hien 07/25/2011: fix code>
        request.setAttribute("errorMessage", message);
        //---<Hien 07/25/2011: fix code>
        return mapping.findForward(VIEW);
        //+++<Hien 07/25/2011: fix code>
//        } else {
//            return mapping.findForward(FAILURE);
//        }
        //---<Hien 07/25/2011: fix code>
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="select">              
    /**
     * 
     */
    public ActionForward select(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws java.lang.Exception {
        TaskActionBean taskForm = (TaskActionBean) form;
        ArrayList<Task> taskList = new java.util.ArrayList<Task>();
        taskList = taskForm.select(taskForm.getTaskID());
        request.setAttribute("taskListUpdate", taskList);
        return mapping.findForward(FIRST_UPDATE_ACTION); // Forward to admin/UpdateTaskInfo.jsp
    }
// </editor-fold>    

// <editor-fold defaultstate="collapsed" desc="update">               
    /** Action called on Update button click
     */
    public ActionForward update(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws SQLException {
        TaskActionBean taskForm = (TaskActionBean) form;
        String taskID = taskForm.getTaskID();
        String taskName = taskForm.getTaskName();
        String description = taskForm.getDescription();

        int rowUp = taskForm.update(taskID, taskName, description);//??What is rowUp used for?
        //+++<Hien 07/25/2011: fix code>
        //if (rowUp != 0) {             
        //---<Hien 07/25/2011: fix code>        
        ArrayList<Task> taskList = new java.util.ArrayList<Task>();
        taskList = taskForm.view();
        request.setAttribute("taskList", taskList);
        //+++<Hien 07/25/2011: fix code>
        request.setAttribute("errorMessage", String.valueOf(rowUp));
        //---<Hien 07/25/2011: fix code>
        return mapping.findForward(VIEW);
        //+++<Hien 07/25/2011: fix code>            
//        } else {
//            return mapping.findForward(FAILURE);
//        }
        //---<Hien 07/25/2011: fix code>            
    }
// </editor-fold>
}
